/********************************************************
 * Filename: TclNamespace.java
 * Author: LMU
 * Program: TUnit
 * Description: This class is the Namespace Explorer.  
 * Uses the other classes to handle events within the
 * view.  
 * Revision History:
 * 20060904: Initial revision.  Added header later.  
 * *****************************************************/

package edu.lmu.cs.tUnit.view.model;

import java.util.ArrayList;
import java.util.List;

public class TclNamespace extends Model {
	protected List namespaces;
	protected List procedures;
	
	private static IModelVisitor adder = new Adder();
	private static IModelVisitor remover = new Remover();
	
	public TclNamespace() {
		namespaces = new ArrayList();
		procedures = new ArrayList();
	}
	
	private static class Adder implements IModelVisitor {

		/*
		 * @see ModelVisitorI#visitBook(MovingBox, Object)
		 */
		public void visitTclProcedure(TclProcedure proc, Object argument) {
			((TclNamespace) argument).addTclProcedure(proc);
		}

		/*
		 * @see ModelVisitorI#visitMovingBox(MovingBox, Object)
		 */
		public void visitTclNamespace(TclNamespace space, Object argument) {
			((TclNamespace) argument).addTclNamespace(space);
		}

	}

	private static class Remover implements IModelVisitor {
		/*
		 * @see ModelVisitorI#visitBook(MovingBox, Object)
		 */
		public void visitTclProcedure(TclProcedure proc, Object argument) {
			((TclNamespace) argument).removeTclProcedure(proc);
		}

		/*
		 * @see ModelVisitorI#visitMovingBox(MovingBox, Object)
		 */
		public void visitTclNamespace(TclNamespace space, Object argument) {
			((TclNamespace) argument).removeTclNamespace(space);
			space.addListener(NullDeltaListener.getSoleInstance());
		}

	}
	
	public TclNamespace(String name) {
		this();
		this.name = name;
	}
	
	public List getNamespaces() {
		return namespaces;
	}
	
	protected void addTclNamespace(TclNamespace space) {
		namespaces.add(space);
		space.parent = this;
		fireAdd(space);
	}
	
	protected void addTclProcedure(TclProcedure proc) {
		procedures.add(proc);
		proc.parent = this;
		fireAdd(proc);
	}
		
	public List getProcedures() {
		return procedures;
	}
	
	public void remove(Model toRemove) {
		toRemove.accept(remover, this);
	}
		
	protected void removeTclProcedure(TclProcedure proc) {
		procedures.remove(proc);
		proc.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(proc);
	}
	
	protected void removeTclNamespace(TclNamespace space) {
		namespaces.remove(space);
		space.addListener(NullDeltaListener.getSoleInstance());
		fireRemove(space);	
	}

	public void add(Model toAdd) {
		toAdd.accept(adder, this);
	}
		
	/** Answer the total number of items the
	 * receiver contains. */
	public int size() {
		return getProcedures().size() + getNamespaces().size();
	}
	/*
	 * @see Model#accept(ModelVisitorI, Object)
	 */
	public void accept(IModelVisitor visitor, Object passAlongArgument) {
		visitor.visitTclNamespace(this, passAlongArgument);
	}

}
